﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;

namespace BouquetApplication
{
    public partial class EditPlantForm : Form
    {

        const string LilieItem = "Лилия";
        const string RoseItem = "Роза";
        const string GreenItem = "Зелень";

        private AbstractPlant plant;

        private ComponentValidator validator = new ComponentValidator();
        private Dictionary<string, Type> types = new Dictionary<string, Type>();

        public EditPlantForm()
        {
            InitializeComponent();

            types.Add("Лилия", typeof(Lilie));
            types.Add("Роза", typeof(Rosaceous));
            types.Add("Зелень", typeof(Green));

            validator.Add(nameTextBox, new NonEmptyValidator(), "Наименование");      
            validator.Add(priceTextBox, new NonEmptyValidator(), "Цена");
            validator.Add(priceTextBox, new NumberValidator(), "Цена");
            validator.Add(branchCountTextBox, new NonEmptyValidator(), "Количество ветвей");
            validator.Add(branchCountTextBox, new NumberValidator(), "Количество ветвей");

            typeComboBox.Items.Add(LilieItem);
            typeComboBox.Items.Add(RoseItem);
            typeComboBox.Items.Add(GreenItem);
            typeComboBox.SelectedIndex = 0;

            colorComboBox.Items.Add(FlowerColor.Red);
            colorComboBox.Items.Add(FlowerColor.White);
            colorComboBox.Items.Add(FlowerColor.Yellow);
            colorComboBox.SelectedIndex = 0;

            lilieSortComboBox.Items.Add(LilieSort.Tiger);
            lilieSortComboBox.Items.Add(LilieSort.Queen);
            lilieSortComboBox.SelectedIndex = 0;

            budSizeComboBox.Items.Add(RoseSize.Small);
            budSizeComboBox.Items.Add(RoseSize.Medium);
            budSizeComboBox.Items.Add(RoseSize.Big);
            budSizeComboBox.SelectedIndex = 0;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void typeComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            Reset();

            string typeName = typeComboBox.Text;
            Type type = types[typeName];

            nameTextBox.Enabled = true;
            priceTextBox.Enabled = true;

            if (typeof(Flower).IsAssignableFrom(type))
            {
                colorComboBox.Enabled = true;
            }
           
            if (typeof(Lilie).IsAssignableFrom(type))
            { 
                lilieSortComboBox.Enabled = true;
            }

            if (typeof(Rosaceous).IsAssignableFrom(type))
            {
                budSizeComboBox.Enabled = true;
            }

            if (typeof(Green).IsAssignableFrom(type))
            {
                branchCountTextBox.Enabled = true;
            }

        }

        private void Reset()
        {
            nameTextBox.Enabled = false;
            priceTextBox.Enabled = false;
            budSizeComboBox.Enabled = false;
            lilieSortComboBox.Enabled = false;
            branchCountTextBox.Enabled = false;
            colorComboBox.Enabled = false;
        }

        private void okButton_Click(object sender, EventArgs e)
        {

            string errorMessage = validator.Validate();
            if (errorMessage != null && errorMessage.Length > 0)
            {
                MessageBox.Show(errorMessage);
                return;
            }
              

            string typeName =  typeComboBox.Text;
            Type type = types[typeName];

            plant = (AbstractPlant) type.GetConstructor(new Type[] {}).Invoke(new object[] {});

            plant.Name = nameTextBox.Text;
            plant.Price = Int32.Parse(priceTextBox.Text);

            if (typeof(Flower).IsAssignableFrom(type))
            {
                Flower flower = (Flower)plant;
                flower.Color = (FlowerColor)colorComboBox.SelectedItem;
            }

            if (typeof(Lilie).IsAssignableFrom(type))
            {
                Lilie lilie = (Lilie)plant;
                lilie.Color = (FlowerColor) colorComboBox.SelectedItem;
                lilie.Sort = (LilieSort)lilieSortComboBox.SelectedItem;
            }

            if (typeof(Rosaceous).IsAssignableFrom(type))
            {
                Rosaceous rosaceous = (Rosaceous)plant;
                rosaceous.Color = (FlowerColor)colorComboBox.SelectedItem;
                rosaceous.BudSize = (RoseSize)budSizeComboBox.SelectedItem;
            }

            if (typeof(Green).IsAssignableFrom(type))
            {
                Green green = (Green)plant;
                green.Name = nameTextBox.Text;
                green.Price = Int32.Parse(priceTextBox.Text);
                green.BranchCount = Int32.Parse(branchCountTextBox.Text);
            }          

            this.Close();


        }

        public AbstractPlant Plant
        {
            get { return plant; }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void colorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lilieSortComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void EditPlantForm_Load(object sender, EventArgs e)
        {

        }

    }
}
